//
//  JTTYPGViewController.m
//  Estate
//
//  Created by houzhen on 2017/11/4.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import <MJExtension.h>
static CGFloat const ButtonHeight = 40;
#import "JJTYPZZLViewController.h"
#import "JJTYPZRViewController.h"
#import "JJTYPZWLViewController.h"
#import "JJTYPZJXViewController.h"

/**6种法环*/
#import "JJPZFHBaseViewController.h"
#import "JJTYPZFHViewController.h"
#import "JJLMPZViewController.h"
#import "JJSPPZViewController.h"
#import "JJYJPZViewController.h"
#import "JJZLPZViewController.h"
#import "JJZJPZViewController.h"
#import "SegmentViewController.h"

#import "JJTYPZViewController.h"


@interface JJTYPZViewController ()
{
    FMDatabase *_db;
    DBHelper *_helper;
    SegmentViewController *segmentVC;
}

/**基本资料*/
@property(nonatomic,strong)JJTYPZZLViewController* ZLCtr;
/**人*/
@property(nonatomic,strong)JJTYPZRViewController* RCtr;
/**机械*/
@property(nonatomic,strong)JJTYPZJXViewController* JXCtr;
/**物料*/
@property(nonatomic,strong)JJTYPZWLViewController* WLCtr;
/**法环*/
@property(nonatomic,strong)JJPZFHBaseViewController* FHCtr;

@property (assign, nonatomic)BOOL edit;

/**附件*/
@property (copy, nonatomic) NSArray *fileArray;

// 附件ID
@property (copy, nonatomic) NSMutableArray *peopleLJArr;
@property (copy, nonatomic) NSMutableArray *machineryLJArr;
@property (copy, nonatomic) NSMutableArray *materialLJArr;
@property (copy, nonatomic) NSMutableArray *ringLJArr;


@end


@implementation JJTYPZViewController

- (NSMutableArray *)peopleLJArr {
    if (!_peopleLJArr) {
        _peopleLJArr = [[NSMutableArray alloc] init];
    }
    return _peopleLJArr;
}

- (NSMutableArray *)machineryLJArr {
    if (!_machineryLJArr) {
        _machineryLJArr = [[NSMutableArray alloc] init];
    }
    return _machineryLJArr;
}

- (NSMutableArray *)materialLJArr {
    if (!_materialLJArr) {
        _materialLJArr = [[NSMutableArray alloc] init];
    }
    return _materialLJArr;
}

- (NSMutableArray *)ringLJArr {
    if (!_ringLJArr) {
        _ringLJArr = [[NSMutableArray alloc] init];
    }
    return _ringLJArr;
}

// 基本资料模型
- (PZBasicModel *)basicModel {
    if (!_basicModel) {
        _basicModel = [[PZBasicModel alloc] init];
    }
    return _basicModel;
}

// 人模型
- (JJTYPersonModel *)person {
    if (!_person) {
        _person = [[JJTYPersonModel alloc] init];
    }
    return _person;
}

// 机械模型
- (JJTYMachineryModel *)machinery {
    if (!_machinery) {
        _machinery = [[JJTYMachineryModel alloc] init];
    }
    return _machinery;
}

// 物料模型
- (JJTYMaterialModel *)material {
    if (!_material) {
        _material = [[JJTYMaterialModel alloc] init];
    }
    return _material;
}

//获取交建详细数据
- (void)setSideId:(NSString *)sideId {
    _sideId = sideId;
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    DBHelper *helper=[DBHelper help];

    NSDictionary *supData = [[helper DataBase:db selectKeyTypes:@{@"ID":@"text", @"PRO_ID":@"text", @"PRO_NAME":@"text", @"RECORD_USER":@"text", @"RECORD_MASTER":@"text", @"RECORD_SUPERVISION_USER":@"text", @"WEATHER":@"text", @"ADDRESS_ID":@"text", @"ADDRES_INFO":@"text", @"RECORD_TYPE":@"text", @"RECORD_TITLE":@"text", @"RECORD_NUMBER":@"text", @"RECORD_INFO":@"text", @"RECORD_FILE_PATH":@"text", @"RECORDONTENT":@"text", @"STATE":@"text", @"SUPERVISOR_PRO_UNIT_ID":@"text", @"CONSTRUCT_PRO_UNIT_ID":@"text", @"SUPERVISOR_PRO_UNIT_NAME":@"text", @"CONSTRUCT_PRO_UNIT_NAME":@"text", @"SUPERVISOR_CONTRACT_NUMBER":@"text", @"CONSTRUCT_CONTRACT_NUMBER":@"text", @"SIDE_START_TIME":@"text", @"SIDE_END_TIME":@"text", @"SITE_NAME":@"text", @"RANGE_AND_POSITION":@"text", @"EXTEND_INFO":@"text", @"CREATE_TIME":@"text", @"CREATE_USER_NAME":@"text", @"CREATE_IP":@"text"} fromTable:WORK_RECORD whereCondition:@{@"ID":sideId}]lastObject];
    
    // 基本信息
    self.basicModel.recordID = _sideId;
    self.basicModel.proID = supData[@"PRO_ID"];
    self.basicModel.proName = supData[@"PRO_NAME"];
    self.basicModel.recordUser = supData[@"RECORD_USER"];
    self.basicModel.masterUser = supData[@"RECORD_MASTER"];
    self.basicModel.jlName = supData[@"RECORD_SUPERVISION_USER"];
    self.basicModel.weather = supData[@"WEATHER"];
    self.basicModel.locationId = DEFISNUL(supData[@"ADDRESS_ID"]);
    self.basicModel.location = DEFISNUL(supData[@"ADDRES_INFO"]);
    self.basicModel.recordType = supData[@"RECORD_TYPE"];
    self.basicModel.recordTitle = supData[@"RECORD_TITLE"];
    self.basicModel.number = supData[@"RECORD_NUMBER"];
    self.basicModel.recordOntent = supData[@"RECORDONTENT"];
    self.basicModel.state = supData[@"STATE"];
    self.basicModel.shigongDanweiID = supData[@"CONSTRUCT_PRO_UNIT_ID"];
    self.basicModel.jianliDanweiID =  supData[@"SUPERVISOR_PRO_UNIT_ID"];
    self.basicModel.shigongDanwei = supData[@"CONSTRUCT_PRO_UNIT_NAME"];
    self.basicModel.jianliDanwei =  supData[@"SUPERVISOR_PRO_UNIT_NAME"];
    self.basicModel.contrctNumber = supData[@"SUPERVISOR_CONTRACT_NUMBER"];
    self.basicModel.constructionNumber = supData[@"CONSTRUCT_CONTRACT_NUMBER"];
    self.basicModel.beginTime = supData[@"SIDE_START_TIME"];
    self.basicModel.endTime = supData[@"SIDE_END_TIME"];
    self.basicModel.siteName = supData[@"SITE_NAME"];
    self.basicModel.otherAddress = supData[@"RANGE_AND_POSITION"];
    self.basicModel.extendInfo = supData[@"EXTEND_INFO"];
    self.basicModel.createUser = supData[@"CREATE_USER_NAME"];
    self.basicModel.createTime = supData[@"CREATE_TIME"];
    self.basicModel.createIP = supData[@"CREATE_IP"];
    
    // 其他信息
    NSString *dataJson = supData[@"RECORD_INFO"];
    //详细数据转字典
    NSDictionary *siteDic = [LSUtility dictionaryWithJsonString:dataJson];
    self.person = [JJTYPersonModel yy_modelWithDictionary:siteDic[@"person"]];
    self.machinery = [JJTYMachineryModel yy_modelWithDictionary:siteDic[@"machinery"]];
    self.material = [JJTYMaterialModel yy_modelWithDictionary:siteDic[@"material"]];
    
    //法环
    NSDictionary *FHDic = siteDic[@"fahuan"];
    //法环为不同界面  根据标识以示区别
    if ([self.isSubStr isEqualToString:@"RECORD_02_JJ_01"]) {
        self.fahuanTY = [JJTYFahuanModel mj_objectWithKeyValues:FHDic];
        NSLog(@"%@", self.fahuanTY);
        
    } else if ([self.isSubStr isEqualToString:@"RECORD_02_JJ_02"]) {
        self.fahuanZJ = [JJZJFaHuanModel mj_objectWithKeyValues:FHDic];
        
    } else if ([self.isSubStr isEqualToString:@"RECORD_02_JJ_03"]) {
        self.fahuanSP = [JJSPFaHuanModel mj_objectWithKeyValues:FHDic];
        
    } else if ([self.isSubStr isEqualToString:@"RECORD_02_JJ_04"]) {
        self.fahuanLM = [JJLMFaHuanModel mj_objectWithKeyValues:FHDic];
        
    } else if ([self.isSubStr isEqualToString:@"RECORD_02_JJ_05"]) {
        self.fahuanZL = [JJZLFaHuanModel mj_objectWithKeyValues:FHDic];
        
    } else if ([self.isSubStr isEqualToString:@"RECORD_02_JJ_06"]) {
        self.fahuanYJ = [JJYJFaHuanModel mj_objectWithKeyValues:FHDic];
    }
    
    // 得到附件数组
    NSString *fileJson = DEFISNUL(supData[@"RECORD_FILE_PATH"]);
    self.fileArray = [LSUtility dictionaryWithJsonString:fileJson];
}

//解决查看后 再次添加 无法显示保存按钮的bug
- (void)setFlag:(NSString *)flag {
    _flag=flag;
    if([self.flag isEqualToString:@"addty"]){
        self.edit=YES;
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
        
        // 日记页面链接进入
    } else if ([self.flag isEqualToString:@"look"]){
        self.edit = NO;
        
    } else if ([self.flag isEqualToString:@"upRecord"]){
        //旁站列表页进入
        //判断是否超过旁站结束时间 或者 日记提取了此条记录
        if (self.basicModel.createTime && ![self.basicModel.createTime isEqualToString:@""] && self.basicModel.endTime && ![self.basicModel.endTime isEqualToString:@""]) {
            self.edit = [LSUtility siteEditForendTimeAndSiteRecordId:self.sideId beginTine:self.basicModel.createTime endTime:self.basicModel.endTime];
        }
        if(self.edit){
            //添加右上方提交按钮
            self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
            
        }else{
            self.navigationItem.rightBarButtonItem=nil;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    _helper=[DBHelper help];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.view endEditing:YES];
    [super viewWillAppear:animated];
    //根据flag 选择性刷新页面
    if(self.isSubStr!=nil){
        [self createsliderBtn];
    }
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //清除flag：确保进入二级页面后 返回数据不会清空
    self.isSubStr = nil;
}

#pragma mark - 创建Slider按钮
- (void)createsliderBtn {
    WEAKSELF
    if (segmentVC) {
        [segmentVC.view removeFromSuperview];
    }
    segmentVC = [[SegmentViewController alloc]init];
    segmentVC.height = DEF_SCREEN_HEIGHT - 108;
    segmentVC.titleArray = @[@"基本资料",@"人",@"机械",@"物料",@"法/环"];
    
    //此四个界面为通用界面
    weakSelf.ZLCtr = [[JJTYPZZLViewController alloc]init];
    weakSelf.RCtr = [[JJTYPZRViewController alloc]init];
    weakSelf.JXCtr = [[JJTYPZJXViewController alloc]init];
    weakSelf.WLCtr = [[JJTYPZWLViewController alloc]init];
    
    //法环为不同界面  根据标识以示区别
    if ([weakSelf.isSubStr isEqualToString:@"RECORD_02_JJ_01"]) {
        weakSelf.FHCtr = [[JJTYPZFHViewController alloc]init];
        weakSelf.FHCtr.fahuanTY = weakSelf.fahuanTY;
    }else if ([weakSelf.isSubStr isEqualToString:@"RECORD_02_JJ_02"]) {
        weakSelf.FHCtr = [[JJZJPZViewController alloc]init];
        weakSelf.FHCtr.fahuanZJ = weakSelf.fahuanZJ;
        
    }else if ([weakSelf.isSubStr isEqualToString:@"RECORD_02_JJ_03"]) {
        weakSelf.FHCtr = [[JJSPPZViewController alloc]init];
        weakSelf.FHCtr.fahuanSP = weakSelf.fahuanSP;
        
    }else if ([weakSelf.isSubStr isEqualToString:@"RECORD_02_JJ_04"]) {
        weakSelf.FHCtr = [[JJLMPZViewController alloc]init];
        weakSelf.FHCtr.fahuanLM = weakSelf.fahuanLM;
        
    }else if ([weakSelf.isSubStr isEqualToString:@"RECORD_02_JJ_05"]) {
        weakSelf.FHCtr = [[JJZLPZViewController alloc]init];
        weakSelf.FHCtr.fahuanZL = weakSelf.fahuanZL;
        
    }else if ([weakSelf.isSubStr isEqualToString:@"RECORD_02_JJ_06"]) {
        weakSelf.FHCtr = [[JJYJPZViewController alloc]init];
        weakSelf.FHCtr.fahuanYJ = weakSelf.fahuanYJ;
    }
    weakSelf.FHCtr.isSubStr = weakSelf.isSubStr;
    self.ZLCtr.isSubStr = weakSelf.isSubStr;
    
    //查看详情 设置数据model
    self.ZLCtr.basicModel = self.basicModel;
    self.RCtr.person = self.person;
    self.JXCtr.machinery = self.machinery;
    self.WLCtr.material = self.material;
    // 附件
    self.RCtr.fileArray = self.fileArray;
    self.JXCtr.fileArray = self.fileArray;
    self.WLCtr.fileArray = self.fileArray;
    self.FHCtr.fileArray = self.fileArray;

    // ID
    self.RCtr.sideId = self.sideId;
    self.JXCtr.sideId = self.sideId;
    self.WLCtr.sideId = self.sideId;
    self.FHCtr.sideId = self.sideId;
    
    // 查看页面
    if(![weakSelf.flag isEqualToString:@"addty"]){
        weakSelf.ZLCtr.isEdit=weakSelf.edit;
        weakSelf.RCtr.isEdit=weakSelf.edit;
        weakSelf.JXCtr.isEdit=weakSelf.edit;
        weakSelf.WLCtr.isEdit=weakSelf.edit;
        weakSelf.FHCtr.isEdit=weakSelf.edit;
        
    } else {
        //添加页面 允许编辑
        weakSelf.ZLCtr.isEdit=YES;
        weakSelf.RCtr.isEdit=YES;
        weakSelf.JXCtr.isEdit=YES;
        weakSelf.WLCtr.isEdit=YES;
        weakSelf.FHCtr.isEdit=YES;
    }
    
    NSMutableArray *controlArray = [[NSMutableArray alloc]initWithObjects:weakSelf.ZLCtr,weakSelf.RCtr,weakSelf.JXCtr,weakSelf.WLCtr, weakSelf.FHCtr,nil];
    //设置选中字体颜色
    segmentVC.titleSelectedColor = [TTTheme defaultManger].defaultColor;
    //设置子视图
    segmentVC.subViewControllers = controlArray;
    //标签栏背景色
    segmentVC.headViewBackgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    //非选中状态下标签字体大小
    segmentVC.fontSize=14.;
    //选中下划线颜色
    segmentVC.bottomLineColor=[TTTheme defaultManger].defaultColor;
    segmentVC.buttonWidth = DEF_SCREEN_WIDTH/5;
    segmentVC.buttonHeight = ButtonHeight;
    [segmentVC initSegment];
    [segmentVC addParentController:self];
}

#pragma  mark- 保存方法
- (void)clickSave {
    if (self.basicModel.siteName == nil) {
        [LSUtility showToastWithMessage:@"旁站项目不能为空" inView:self.view Andtime:1.];
    }
    else if(!self.basicModel.shigongDanwei){
        [LSUtility showToastWithMessage:@"请选择施工单位" inView:self.view Andtime:1.];
    }
    else if (self.basicModel.location == nil)
    {
        [LSUtility showToastWithMessage:@"位置信息不能为空" inView:self.view Andtime:1.];
    }
    else
    {
        if([self.flag isEqualToString:@"addty"]){
            [self addDataBase];
            
        }else if ([self.flag isEqualToString:@"upRecord"]){
            [self upDataBase];
            
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

#pragma mark - 新增数据
- (void)addDataBase {
    NSString *recordId=[NSUUID UUID].UUIDString;
    NSMutableArray *fileArr = [self insertAttachment:recordId];
    NSString *recordInFoPath = [LSUtility dictionaryToJson_2:fileArr];
    
    // 数据结构法，存入数据库
    // 人
    NSMutableDictionary *personDic = [self.person mj_keyValues];
    NSString *personStr = [LSUtility dictionaryToJson:self.peopleLJArr];
    [personDic setObject:personStr forKey:@"items"];
    // 机械
    NSMutableDictionary *machineryDic = [self.machinery mj_keyValues];
    NSString *machineryStr = [LSUtility dictionaryToJson:self.machineryLJArr];
    [machineryDic setObject:machineryStr forKey:@"items"];
    // 物料
    NSMutableDictionary *materialDic = [self.material mj_keyValues];
    NSString *materialStr = [LSUtility dictionaryToJson:self.materialLJArr];
    [materialDic setObject:materialStr forKey:@"items"];
    // 法环
    NSMutableDictionary *FHDic = [NSMutableDictionary dictionary];
    if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_01"]) {
        FHDic = [self.FHCtr.fahuanTY mj_keyValues];
        
    }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_02"]) {
        FHDic = [self.FHCtr.fahuanZJ mj_keyValues];
        
    }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_03"]) {
        FHDic = [self.FHCtr.fahuanSP mj_keyValues];
        
    }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_04"]) {
        FHDic = [self.FHCtr.fahuanLM mj_keyValues];
        
    }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_05"]) {
        FHDic = [self.FHCtr.fahuanZL mj_keyValues];
        
    }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_06"]) {
        FHDic = [self.FHCtr.fahuanYJ mj_keyValues];
    }
    NSString *fahuanStr = [LSUtility dictionaryToJson:self.ringLJArr];
    [FHDic setObject:fahuanStr forKey:@"items"];
    
    // 工作记录表新增数据(字典转json   RECORD_INFO)
    NSDictionary *dataDic = @{@"person":personDic, @"machinery":machineryDic, @"material":materialDic, @"fahuan":FHDic};
    NSString *strData = [LSUtility dictionaryToJson:dataDic];
    NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
    if([role isEqualToString:@"USER_01"]){
        role=@"0";
        
    }else if ([role isEqualToString:@"USER_03"]||[role isEqualToString:@"USER_07"]){
        role=@"1";
    }
    // 旁站标题
    if(!self.basicModel.otherAddress){
        self.basicModel.otherAddress = @"";
    }
    if(!self.basicModel.siteName){
        self.basicModel.siteName = @"";
    }
    NSString *title = [NSString stringWithFormat:@"%@_%@", DEFISNUL(self.basicModel.otherAddress), DEFISNUL(self.basicModel.siteName)];
    
    // 字段及对应值
    NSDictionary *pragameDic=[NSDictionary dictionary];
    pragameDic=@{
                 @"ID":recordId,
                 @"PRO_ID":PROID,
                 @"PRO_NAME":DEFISNUL(self.basicModel.proName),
                 @"RECORD_USER":USERID,
                 @"RECORD_MASTER":@"",
                 @"RECORD_SUPERVISION_USER":@"",
                 @"WEATHER":DEFISNUL(self.basicModel.weather),
                 @"ADDRESS_ID":DEFISNUL(self.basicModel.locationId),
                 @"ADDRES_INFO":DEFISNUL(self.basicModel.location),
                 @"RECORD_TYPE":DEFISNUL(self.FHCtr.isSubStr),
                 @"RECORD_TITLE":DEFISNUL(title),
                 @"RECORD_NUMBER":DEFISNUL(self.basicModel.number),
                 @"RECORD_FILE_PATH":DEFISNUL(recordInFoPath),
                 @"RECORD_INFO":DEFISNUL(strData),
                 @"RECORDONTENT":@"",
                 @"STATE":role,
                 @"SUPERVISOR_PRO_UNIT_ID":DEFISNUL(self.basicModel.jianliDanweiID),
                 @"CONSTRUCT_PRO_UNIT_ID":DEFISNUL(self.basicModel.shigongDanweiID),
                 @"SUPERVISOR_PRO_UNIT_NAME":DEFISNUL(self.basicModel.jianliDanwei),
                 @"CONSTRUCT_PRO_UNIT_NAME":DEFISNUL(self.basicModel.shigongDanwei),
                 @"SUPERVISOR_CONTRACT_NUMBER":DEFISNUL(self.basicModel.contrctNumber),
                 @"CONSTRUCT_CONTRACT_NUMBER":DEFISNUL(self.basicModel.constructionNumber),
                 @"SIDE_START_TIME":DEFISNUL(self.basicModel.beginTime),
                 @"SIDE_END_TIME":DEFISNUL(self.basicModel.endTime),
                 @"SITE_NAME":DEFISNUL(self.basicModel.siteName),
                 @"RANGE_AND_POSITION":DEFISNUL(self.basicModel.otherAddress),
                 @"EXTEND_INFO":@"",
                 @"CREATE_USER_NAME":DEF_PERSISTENT_GET_OBJECT(@"loginuser"),
                 @"CREATE_TIME":DEFISNUL([LSUtility nowTime]),
                 @"CREATE_IP":DEFISNUL([LSUtility getIPAddress]),
                 @"MODIFY_USER_NAME":@"",
                 @"MODIFY_TIME":@"",
                 @"MODIFY_IP":@"",
                 @"LOCAL_MODIFY_USER_NAME":@"",
                 @"LOCAL_MODIFY_TIME":@"",
                 @"LOCAL_MODIFY_STATE":@"ADD",
                 };
    
    BOOL isAdd=[_helper DataBase:_db insertKeyValues:pragameDic intoTable:WORK_RECORD];
    if(isAdd){
        NSLog(@"交建-旁站新增成功");
        NSNotification * notice = [NSNotification notificationWithName:@"Refresh_jl_pz" object:nil userInfo:nil];
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        NSLog(@"交建-旁站新增失败");
    }
}

#pragma mark - 修改数据
- (void)upDataBase {
    NSMutableArray *fileArr = [self insertAttachment:_sideId];
    NSString *recordInFoPath=[LSUtility dictionaryToJson_2:fileArr];
    if (self.edit) {

        // 数据结构法，存入数据库
        // 人
        NSMutableDictionary *personDic = [self.person mj_keyValues];
        NSString *personStr = [LSUtility dictionaryToJson:self.peopleLJArr];
        [personDic setObject:personStr forKey:@"items"];
        // 机械
        NSMutableDictionary *machineryDic = [self.machinery mj_keyValues];
        NSString *machineryStr = [LSUtility dictionaryToJson:self.machineryLJArr];
        [machineryDic setObject:machineryStr forKey:@"items"];
        // 物料
        NSMutableDictionary *materialDic = [self.material mj_keyValues];
        NSString *materialStr = [LSUtility dictionaryToJson:self.materialLJArr];
        [materialDic setObject:materialStr forKey:@"items"];
        // 法环
        NSMutableDictionary *FHDic = [NSMutableDictionary dictionary];
        if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_01"]) {
            FHDic = [self.FHCtr.fahuanTY mj_keyValues];
            
        }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_02"]) {
            FHDic = [self.FHCtr.fahuanZJ mj_keyValues];
            
        }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_03"]) {
            FHDic = [self.FHCtr.fahuanSP mj_keyValues];
            
        }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_04"]) {
            FHDic = [self.FHCtr.fahuanLM mj_keyValues];
            
        }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_05"]) {
            FHDic = [self.FHCtr.fahuanZL mj_keyValues];
            
        }else if ([self.FHCtr.isSubStr isEqualToString:@"RECORD_02_JJ_06"]) {
            FHDic = [self.FHCtr.fahuanYJ mj_keyValues];
        }
        NSString *fahuanStr = [LSUtility dictionaryToJson:self.ringLJArr];
        [FHDic setObject:fahuanStr forKey:@"items"];
        
        //字典转json   RECORD_INFO
        NSDictionary *dataDic = @{@"person":personDic, @"machinery":machineryDic, @"material":materialDic, @"fahuan":FHDic};
        NSString *strData=[LSUtility dictionaryToJson:dataDic];
        NSString *title = [NSString stringWithFormat:@"%@_%@", DEFISNUL(self.basicModel.otherAddress), DEFISNUL(self.basicModel.siteName)];

        //字段及对应值
        NSDictionary *pragameDic=[NSDictionary dictionary];
        pragameDic=@{
                     @"PRO_ID":DEFISNUL(self.basicModel.proID),
                     @"PRO_NAME":DEFISNUL(self.basicModel.proName),
                     @"RECORD_USER":DEFISNUL(self.basicModel.recordUser),
                     @"RECORD_MASTER":DEFISNUL(self.basicModel.masterUser),
                     @"RECORD_SUPERVISION_USER":DEFISNUL(self.basicModel.jlName),
                     @"WEATHER":DEFISNUL(self.basicModel.weather),
                     @"ADDRESS_ID":DEFISNUL(self.basicModel.locationId),
                     @"ADDRES_INFO":DEFISNUL(self.basicModel.location),
                     @"RECORD_TYPE":DEFISNUL(self.FHCtr.isSubStr),
                     @"RECORD_TITLE":DEFISNUL(title),
                     @"RECORDONTENT":DEFISNUL(self.basicModel.recordOntent),
                     @"RECORD_NUMBER":DEFISNUL(self.basicModel.number),
                     @"RECORD_FILE_PATH":DEFISNUL(recordInFoPath),
                     @"RECORD_INFO":DEFISNUL(strData),
                     @"STATE":DEFISNUL(self.basicModel.state),
                     @"SUPERVISOR_PRO_UNIT_ID":DEFISNUL(self.basicModel.jianliDanweiID),
                     @"CONSTRUCT_PRO_UNIT_ID":DEFISNUL(self.basicModel.shigongDanweiID),
                     @"SUPERVISOR_PRO_UNIT_NAME":DEFISNUL(self.basicModel.jianliDanwei),
                     @"CONSTRUCT_PRO_UNIT_NAME":DEFISNUL(self.basicModel.shigongDanwei),
                     @"SUPERVISOR_CONTRACT_NUMBER":DEFISNUL(self.basicModel.contrctNumber),
                     @"CONSTRUCT_CONTRACT_NUMBER":DEFISNUL(self.basicModel.constructionNumber),
                     @"SIDE_START_TIME":DEFISNUL(self.basicModel.beginTime),
                     @"SIDE_END_TIME":DEFISNUL(self.basicModel.endTime),
                     @"SITE_NAME":DEFISNUL(self.basicModel.siteName),
                     @"RANGE_AND_POSITION":DEFISNUL(self.basicModel.otherAddress),
                     @"EXTEND_INFO":DEFISNUL(self.basicModel.extendInfo),
                     @"MODIFY_USER_NAME":DEF_PERSISTENT_GET_OBJECT(@"loginuser"),
                     @"MODIFY_TIME":DEFISNUL([LSUtility nowTime]),
                     @"MODIFY_IP":DEFISNUL([LSUtility getIPAddress]),
                     @"LOCAL_MODIFY_STATE":@"MOD",
                     };
        
        [_helper DataBase:_db updateTable:WORK_RECORD setKeyValues:pragameDic whereCondition:@{@"ID":_sideId}];
        
        NSNotification *notice = [NSNotification notificationWithName:@"Refresh_jl_pz" object:nil userInfo:nil];
        [[NSNotificationCenter defaultCenter]postNotification:notice];
    } else {
        //无法编辑，不做任何操作
    }
    [self.navigationController popViewControllerAnimated:YES];
}

//附件存入
- (NSMutableArray *)insertAttachment:(NSString *)recordIds {
    NSMutableArray *recordFileArr = [NSMutableArray array];
    
    //人附件存入附件表
    self.RCtr.selectedAssetURLs = [self.RCtr.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
    if (self.RCtr.selectedAssetURLs.count>0) {
        NSMutableArray *rFileArr=[LSUtility insertAttachmentSiteFromArray:self.RCtr.selectedAssetURLs andRecordId:recordIds andType:self.FHCtr.isSubStr andChildType:@"RECORD_02_SIDE_01"];
        
        for (NSDictionary *dic in rFileArr) {
            NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
            [mDic setObject:dic[@"fileId"] forKey:@"imageId"];
            [self.peopleLJArr addObject:mDic];
            [recordFileArr addObject:dic];
        }
    }
    
    //机械附件存入
    self.JXCtr.selectedAssetURLs = [self.JXCtr.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
    if(self.JXCtr.selectedAssetURLs.count>0){
        NSMutableArray *JXFileArr=[LSUtility insertAttachmentSiteFromArray:self.JXCtr.selectedAssetURLs andRecordId:recordIds andType:self.FHCtr.isSubStr andChildType:@"RECORD_02_SIDE_02"];
        for (NSDictionary *dic in JXFileArr) {
            NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
            [mDic setObject:dic[@"fileId"] forKey:@"imageId"];
            [self.machineryLJArr addObject:mDic];
            [recordFileArr addObject:dic];
        }
    }
    
    //物料附件存入
    self.WLCtr.selectedAssetURLs = [self.WLCtr.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
    if(self.WLCtr.selectedAssetURLs.count>0){
        NSMutableArray *wlFileArr=[LSUtility insertAttachmentSiteFromArray:self.WLCtr.selectedAssetURLs andRecordId:recordIds andType:self.FHCtr.isSubStr andChildType:@"RECORD_02_SIDE_03"];
        for (NSDictionary *dic in wlFileArr) {
            NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
            [mDic setObject:dic[@"fileId"] forKey:@"imageId"];
            [self.materialLJArr addObject:mDic];
            [recordFileArr addObject:dic];
        }
    }
    
    //法环附件存入
    self.FHCtr.selectedAssetURLs = [self.FHCtr.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
    
    if (self.FHCtr.selectedAssetURLs.count>0) {
        NSMutableArray *fhFileArr=[LSUtility insertAttachmentSiteFromArray:self.FHCtr.selectedAssetURLs andRecordId:recordIds andType:self.FHCtr.isSubStr andChildType:@"RECORD_02_SIDE_04"];
        
        for (NSDictionary *dic in fhFileArr) {
            NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
            [mDic setObject:dic[@"fileId"] forKey:@"imageId"];
            [self.ringLJArr addObject:mDic];
            [recordFileArr addObject:dic];
        }
    }
    return recordFileArr;
}


@end

